package com.maxd.service.helpTask;


import com.maxd.model.*;
import com.maxd.respository.helpTask.HelpSendOrderJpaRepository;
import com.maxd.respository.helpTask.HelpSendOrderRepository;
import com.maxd.respository.helpTask.HelpTaskRepository;
import com.maxd.respository.timedTask.TimedTaskJpaRepository;
import com.maxd.respository.timedTask.TimedTaskRepository;
import com.maxd.respository.userinfo.UserRepository;
import com.maxd.service.message.IMessageService;
import com.maxd.service.money.IMoneyDetailsService;
import com.maxd.service.userMoney.UserMoneyDetailsService;
import com.maxd.service.userinfo.UserServiceImpl;
import com.maxd.utils.Config;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * 接单信息
 */
@Service
public class HelpSendOrderServiceImpl implements HelpSendOrderService {

    /** 接单信息 */
    @Autowired
    private HelpSendOrderJpaRepository helpSendOrderJpaRepository;
    /** 接单信息自定义方法类 */
    @Autowired
    private HelpSendOrderRepository helpSendOrderRepository;
    /** 助力信息自定义方法类 */
    @Autowired
    private HelpTaskRepository helpTaskRepository;
    /** 定时任务 */
    @Autowired
    private TimedTaskJpaRepository timedTaskJpaRepository;
    /** 用户信息 */
    @Autowired
    private UserRepository userRepository;
    /** 用户钱包明细 */
    @Autowired
    private UserMoneyDetailsService userMoneyDetailsService;
    /** 定时任务自定义方法类 */
    @Autowired
    private TimedTaskRepository timedTaskRepository;
    /** 信誉分 */
    @Autowired
    private HelpUserScoreService helpUserScoreService;
    /** 信誉明细分 */
    @Autowired
    private HelpUserScoreDetailsService helpUserScoreDetailsService;
    @Autowired
    private IMessageService iMessageService;
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private IMoneyDetailsService moneyDetailsService;

    private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    @Override
    public Double sumMoneyBySend(String time, Integer flag) {
        if(flag==1){
            return helpSendOrderRepository.sumMoneyBySend1(time);
        }else if(flag==2){
            return helpSendOrderRepository.sumMoneyBySend2(time);
        }else{
            return helpSendOrderRepository.sumMoneyBySend3(time);
        }
    }

    @Override
    public Integer countBySend(String time, Integer flag) {
        if(flag==1){
            return helpSendOrderRepository.countBySend1(time);
        }else if(flag==2){
            return helpSendOrderRepository.countBySend2(time);
        }else{
            return helpSendOrderRepository.countBySend3(time);
        }
    }


    @Override
    public Page<Map<String,Object>> selectSendOrderByTaskId(Pageable pageable, Long helpTaskId) {
        return helpSendOrderRepository.selectSendOrderByTaskId(pageable,helpTaskId);
    }

    @Override
    public Page<Map<String,Object>> selectSendOrderList(Pageable pageable, String phone, Integer state) {
        return helpSendOrderRepository.selectSendOrderList(pageable,phone,state);
    }

    @Override
    public HelpSendOrder selectByHelpTaskId(Long helpTaskId,Long userId) {
        return helpSendOrderRepository.selectByHelpTaskIdAndUserId(helpTaskId,userId);
    }

    @Override
    public Integer selectCountByHelpTaskIdAndUserId(Long helpTaskId, Long userId) {
        return helpSendOrderRepository.selectCountByHelpTaskIdAndUserId(helpTaskId,userId);
    }

    @Transactional
    @Override
    public Integer save(HelpSendOrder helpSendOrder) {
        helpSendOrder.setState(0);
        helpSendOrder.setCreateTime(sdf.format(new Date()));
        //添加接单信息
        helpSendOrderJpaRepository.save(helpSendOrder);
        //助力活动报名人数加一
        helpTaskRepository.updateHelpTaskNum(helpSendOrder.getHelpTaskId());
        //添加定时任务
        HelpTask helpTask=helpTaskRepository.selectById(helpSendOrder.getHelpTaskId());
        TimedTask timedTask=new TimedTask();
        timedTask.setType(2);
        timedTask.setObjectId(helpSendOrder.getId());
        timedTask.setTitle("接单助力活动任务定时");
        timedTask.setCreateTime(new Date());
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MINUTE,helpTask.getRestrictTime());
        timedTask.setEndTime(cal.getTime());
        timedTaskJpaRepository.save(timedTask);
        return 1;
    }

    @Override
    public HelpSendOrder selectById(Long helpSendOrderId) {

        return helpSendOrderRepository.selectById(helpSendOrderId);
    }

    @Override
    @Transactional
    public Integer updateHelpSendOrder(HelpSendOrder helpSendOrder,HelpSendOrder oldHelpSendOrder){
        String date=sdf.format(new Date());
        UserInfo userByWxId = userRepository.getUserById(oldHelpSendOrder.getUserId());
        MessageInfo messageInfo = new MessageInfo();
        if(helpSendOrder.getState()==2){//同意
            //给接单人增加金额
            HelpTask helpTask= helpTaskRepository.selectById(oldHelpSendOrder.getHelpTaskId());
            DecimalFormat formater = new DecimalFormat();
            formater.setMaximumFractionDigits(2);
            formater.setGroupingSize(0);
            formater.setRoundingMode(RoundingMode.FLOOR);
            Double money=Double.parseDouble(formater.format(helpTask.getTaskPrice()));
            userRepository.updateJiFenAdd(money.toString(),oldHelpSendOrder.getUserId());
            MoneyDetails userMoneyDetails=new MoneyDetails();
            userMoneyDetails.setUserId(oldHelpSendOrder.getUserId());
            userMoneyDetails.setTitle("[完成任务]任务ID："+helpTask.getId());
            userMoneyDetails.setContent("增加金额:"+money);
            userMoneyDetails.setType(1);
            userMoneyDetails.setMoney(money);
            userMoneyDetails.setCreateTime(date);
            moneyDetailsService.save(userMoneyDetails);

            //完成接单增加信誉分
            helpUserScoreService.updateUserScore(oldHelpSendOrder.getUserId(),1,1);
            HelpUserScoreDetails helpUserScoreDetails=new HelpUserScoreDetails();
            helpUserScoreDetails.setUserId(oldHelpSendOrder.getUserId());
            helpUserScoreDetails.setTitle("[完成任务]任务Id："+oldHelpSendOrder.getHelpTaskId());
            helpUserScoreDetails.setContent("增加积分：1");
            helpUserScoreDetails.setCreateTime(date);
            helpUserScoreDetails.setScore(1);
            helpUserScoreDetails.setType(1);
            helpUserScoreDetailsService.save(helpUserScoreDetails);
            messageInfo.setContent(userByWxId.getNickName() + " 您好！您的接单任务已通过！");
            if (userByWxId.getClientid() != null) {
                userService.pushToSingle("接单任务", userByWxId.getNickName() + " 您好！您的接单任务已通过！", userByWxId.getClientid());
            }
        }else{
            //拒绝原因
            if(helpSendOrder.getCategory()!=null && helpSendOrder.getCategory()==1){//错误截图  扣除2分
                //扣除信誉分
                helpUserScoreService.updateUserScore(oldHelpSendOrder.getUserId(),2,2);
                HelpUserScoreDetails helpUserScoreDetails=new HelpUserScoreDetails();
                helpUserScoreDetails.setUserId(oldHelpSendOrder.getUserId());
                helpUserScoreDetails.setTitle("[审核失败]任务Id："+oldHelpSendOrder.getHelpTaskId());
                helpUserScoreDetails.setContent("错误截图扣除：2");
                helpUserScoreDetails.setCreateTime(date);
                helpUserScoreDetails.setScore(2);
                helpUserScoreDetails.setType(2);
                helpUserScoreDetailsService.save(helpUserScoreDetails);
                messageInfo.setContent(userByWxId.getNickName() + " 您好！您的接单任务已拒绝！");
                if (userByWxId.getClientid() != null) {
                    userService.pushToSingle("接单任务", userByWxId.getNickName() + " 您好！您的接单任务已拒绝！", userByWxId.getClientid());
                }
            }else if(helpSendOrder.getCategory()!=null && helpSendOrder.getCategory()==2){//伪造截图  扣除3分
                //扣除信誉分
                helpUserScoreService.updateUserScore(oldHelpSendOrder.getUserId(),2,3);
                HelpUserScoreDetails helpUserScoreDetails=new HelpUserScoreDetails();
                helpUserScoreDetails.setUserId(oldHelpSendOrder.getUserId());
                helpUserScoreDetails.setTitle("[审核失败]任务Id："+oldHelpSendOrder.getHelpTaskId());
                helpUserScoreDetails.setContent("伪造截图扣除：3");
                helpUserScoreDetails.setCreateTime(date);
                helpUserScoreDetails.setScore(3);
                helpUserScoreDetails.setType(2);
                helpUserScoreDetailsService.save(helpUserScoreDetails);
                messageInfo.setContent(userByWxId.getNickName() + " 您好！您的接单任务已拒绝！");
                if (userByWxId.getClientid() != null) {
                    userService.pushToSingle("接单任务", userByWxId.getNickName() + " 您好！您的接单任务已拒绝！", userByWxId.getClientid());
                }
            }
        }
        messageInfo.setState(String.valueOf(6));
        messageInfo.setTitle("接单任务");
        messageInfo.setUserName(userByWxId.getNickName());
        messageInfo.setUserId(String.valueOf(userByWxId.getId()));
        iMessageService.saveBody(messageInfo);

        //删除定时任务
        timedTaskRepository.deleteByObjectId(helpSendOrder.getId(),3);
        return helpSendOrderRepository.updateHelpSendOrder(helpSendOrder.getId(),helpSendOrder.getState(),helpSendOrder.getAuditContent(),helpSendOrder.getCategory(),sdf.format(new Date()));
    }

    @Override
    public Integer update(Long id, Integer state){
        return helpSendOrderRepository.update(id,state);
    }

    @Override
    public Integer selectResidueNum(Long helpTaskId,Integer state){
        return helpSendOrderRepository.selectResidueNum(helpTaskId,state);
    }

    @Override
    public Integer endHelpSendOrder(Long helpSendOrderId){
        timedTaskRepository.deleteByObjectId(helpSendOrderId,2);
        return helpSendOrderRepository.update(helpSendOrderId,5);
    }

    @Override
    public Integer selectEndNum(Long helpTaskId) {
        return helpSendOrderRepository.selectEndNum(helpTaskId);
    }

}